package com.jcl.recruit.timer.crawler.service;

import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.core.config.SpringConfig;
import com.jcl.recruit.resume.mapper.ResumeOrigMapper;
import com.jcl.recruit.resume.model.ResumeOrig;
import com.jcl.recruit.searchresume.logic.SearchResumeEduLogic;
import com.jcl.recruit.searchresume.logic.SearchResumeLogic;
import com.jcl.recruit.searchresume.logic.SearchResumeProjectLogic;
import com.jcl.recruit.searchresume.logic.SearchResumeWorkLogic;
import com.jcl.recruit.searchresume.model.SearchResume;
import com.jcl.recruit.searchresume.model.SearchResumeEdu;
import com.jcl.recruit.searchresume.model.SearchResumeProject;
import com.jcl.recruit.searchresume.model.SearchResumeWork;
import com.jcl.recruit.timer.crawler.bean.JxBean;
import com.jcl.recruit.timer.crawler.bean.JxResumeBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CrawlerChannelResumeTaskService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/30 17:41
 * @Version 1.0
 **/
@Service
public class CrawlerChannelResumeTaskService {

    private static final Logger logger = LoggerFactory.getLogger(CrawlerChannelResumeTaskService.class);

    @Autowired
    protected ResumeOrigMapper resumeOrigMapper;

    @Autowired
    protected SearchResumeLogic searchResumeLogic;

    @Autowired
    protected SearchResumeWorkLogic searchResumeWorkLogic;

    @Autowired
    protected SearchResumeEduLogic searchResumeEduLogic;

    @Autowired
    protected SearchResumeProjectLogic searchResumeProjectLogic;

    @Autowired
    protected SpringConfig springConfig;

    public String crawlerChannelResume(String type){

        if(!springConfig.getSpringProfiles().equals("prod")){
            return null;
        }

        List<ResumeOrig> resumeOrigList = resumeOrigMapper.selectResumeOrigsBySource(type);

        // 解析对象
        CrawlerChannelResumeProxy proxy = new CrawlerChannelResumeProxy();
        JxBean jxBean = proxy.crawlerJxBean(type, resumeOrigList);
        List<JxResumeBean> jxResumeBeans = jxBean.getJxResumeBeans();

        Integer success = 0;
        Integer fail = 0;
        List<Integer> idList = jxBean.getExternalIdList();
        loop:
        for(JxResumeBean jxResumeBean: jxResumeBeans){
            SearchResume searchResume = jxResumeBean.getSearchResume();
            if (searchResumeLogic.selectCountByZhiPinSearchResume(searchResume) > 0) {
                fail++;
                continue loop;
            }
            searchResumeLogic.insertSelective(searchResume);
            Integer id = searchResume.getId();

            List<SearchResumeProject> resumeProjects = jxResumeBean.getResumeProjects();
            if(CollectionUtils.isNotEmpty(resumeProjects)){
                for(SearchResumeProject entity: resumeProjects){
                    entity.setResumeId(id);
                }
                searchResumeProjectLogic.batchInsertProjects(resumeProjects);
            }

            List<SearchResumeWork> resumeWorks = jxResumeBean.getResumeWorks();
            if(CollectionUtils.isNotEmpty(resumeWorks)){
                for(SearchResumeWork entity: resumeWorks){
                    entity.setResumeId(id);
                }
                searchResumeWorkLogic.batchInsertWork(resumeWorks);
                searchResume.setWorkLastCompany(resumeWorks.get(0).getCompany());
                searchResume.setWorkLastPosition(resumeWorks.get(0).getPosition());
                searchResume.setCreatetime(null);
                searchResume.setWorkDetail(resumeWorks.get(0).getDuty());
                searchResumeLogic.updateByPrimaryKeySelective(searchResume);
            }

            List<SearchResumeEdu> resumeEdus = jxResumeBean.getSearchResumeEdus();
            if(CollectionUtils.isNotEmpty(resumeEdus)){
                for(SearchResumeEdu entity: resumeEdus){
                    entity.setResumeId(id);
                }
                searchResumeEduLogic.batchInsertEdus(resumeEdus);
                searchResume.setEduLastSchool(resumeEdus.get(0).getSchool());
                searchResume.setEduLastMajor(resumeEdus.get(0).getMajor());
                searchResume.setCreatetime(null);
                searchResumeLogic.updateByPrimaryKeySelective(searchResume);
            }

            success++;
        }

        if(idList.size() > 0) {
            resumeOrigMapper.batchUpdate(idList);
        }
        // 判重目前有问题
//        List<SearchResume> searchResumes = searchResumeLogic.selectChongfu();
//        if(searchResumes.size() > 0) {
//            searchResumeLogic.batchDelete(searchResumes);
//        }
        logger.info("成功添加" + success + "个");
        logger.info("失败" + fail + "个");

        return "成功添加" + success + "个 ----------------------- 重复" + fail + "个";
    }
}
